package com.hxf.web.action.food;

import java.io.IOException;
import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.hxf.common.PublicUtil;
import com.hxf.dto.MealMenuDto;
import com.hxf.dto.MealMenuTypeDto;
import com.hxf.dto.RestaurantDto;
import com.hxf.dto.ReturnData;
import com.hxf.exception.ServiceException;
import com.hxf.web.action.WebActionBase;

/**
 * 餐厅菜肴
 * @author tengyicheng
 *
 */
@Results({@Result(name = "success", location = "../common/success.jsp"), @Result(name = "error", location = "../common/error.jsp"), @Result(name = "msg", location = "../common/msg.jsp")})
public class MealmenuAction extends WebActionBase {

	private static final long serialVersionUID = 1L;
	private MealMenuDto mealMenuDto;
	private List<MealMenuDto> mealMenuList;
	private List<MealMenuTypeDto> mealMenuTypeList;
	private Integer id;
	private String images;
	private String menuName;
	private Integer orderBy;
	private Double price;
	private String introduce;
	private boolean isSell;
	private boolean isRecommend;
	private Double special;
	private Integer mealMenuId;
	private Integer favorableId;
	private Integer taste;
	private Integer menuTypeId;
	private Integer restaurantId;
	private Integer status;
	
	private String ids;
	
	/**
	 * 菜肴列表(餐厅下的菜肴)
	 * @return
	 */
	public String mealMenuList() { 
		try {
			Object adminUserDto = getSession().getAttribute("adminUserDto"); //联盟管理员Session
			Object restaurentUserDto = getSession().getAttribute("restaurentUserDtoLogin");//餐厅 Session
			
			List<RestaurantDto> lstRestDto = null;
			if(null != restaurentUserDto){
				lstRestDto = (List<RestaurantDto>) super.serviceFacade.getRestaurantService().findByRestaurant(getRestaurentUserDto().getId());
			}else if(null != adminUserDto){
				lstRestDto = (List<RestaurantDto>) super.serviceFacade.getRestaurantService().findByRestaurant(getAdminUser().getId());
			}
			
			//List<RestaurantDto> lstRestDto = (List<RestaurantDto>) super.serviceFacade.getRestaurantService().findByRestaurant(getUser().getUserId());
			if (lstRestDto != null && lstRestDto.size()>0) {
				if(null != restaurentUserDto){ // getUser().getUserId() != 1
					RestaurantDto restDto = lstRestDto.get(0);
					if(PublicUtil.isNotNull(getSession().getAttribute("restaurantId"))){
						mealMenuDto.setRestaurantId(Integer.valueOf(getSession().getAttribute("restaurantId").toString()));
					}
					
					//mealMenuDto.setRestaurantId(restDto.getId());
					mealMenuDto.setId(id);//编号查询
					mealMenuDto.setMenuName(menuName);//菜肴名称
					mealMenuDto.setMenuTypeId(menuTypeId);
					ReturnData<MealMenuDto> menuDto = super.serviceFacade.getMealMenuService().findByMealMenu(mealMenuDto, targetPage, pageSize);
					mealMenuList = menuDto.getResultlist();
					this.setRecordCount(menuDto.getRecordcount()); 
					MealMenuTypeDto mmt = new MealMenuTypeDto(); 
					mmt.setRestaurantId(restDto.getId());
					mealMenuTypeList = super.serviceFacade.getMealMenuService().findByMealMenuType(mmt);
				}else if(null != adminUserDto){
					for(int i=0;i<lstRestDto.size();i++){
						RestaurantDto restDto = lstRestDto.get(i);
						mealMenuDto.setRestaurantId(restDto.getId());
						ReturnData<MealMenuDto> menuDto = super.serviceFacade.getMealMenuService().findByMealMenu(mealMenuDto, targetPage, pageSize);
						mealMenuList = menuDto.getResultlist();
						this.setRecordCount(menuDto.getRecordcount()); 
						MealMenuTypeDto mmt = new MealMenuTypeDto(); 
						mmt.setRestaurantId(restDto.getId());
						mealMenuTypeList = super.serviceFacade.getMealMenuService().findByMealMenuType(mmt);
					}
				}
			}
		} catch (ServiceException e) {
			logger.debug(e.getMessage(), e);
		}
		return "meal-menu-list";
	}
	
	/**
	 * 菜肴添加或者修改页面
	 * @return
	 */
	public String mealMenu() {
		try {
			Object adminUserDto = getSession().getAttribute("adminUserDto"); //联盟管理员Session
			Object restaurentUserDto = getSession().getAttribute("restaurentUserDtoLogin");//餐厅 Session 
			
			MealMenuTypeDto mmt = new MealMenuTypeDto();
			RestaurantDto restDto = new RestaurantDto();
			//RestaurantDto restDto = (RestaurantDto)super.serviceFacade.getRestaurantService().findByRestaurant(getUser().getUserId());
			List restDtoList = null;
			if(null != adminUserDto){
				restDtoList = super.serviceFacade.getRestaurantService().findByRestaurant(getAdminUser().getId());
			}else if(null != restaurentUserDto){
				restDtoList = super.serviceFacade.getRestaurantService().findByRestaurant(getRestaurentUserDto().getId());
			}
			
			if (null != restDtoList  ) {
				restDto = (RestaurantDto)restDtoList.get(0);
				mmt.setRestaurantId(restDto.getId());
				mealMenuTypeList = super.serviceFacade.getMealMenuService().findByMealMenuType(mmt);
				if (id != null && id != 0) {
					mealMenuDto = (MealMenuDto)super.serviceFacade.getMealMenuService().get(MealMenuDto.class, id);
					
					getSession().setAttribute("imagePath", mealMenuDto.getImages());
				} else {
					getSession().setAttribute("imagePath", null);
				}
			}
		} catch (ServiceException e) {
			logger.debug(e.getMessage(), e);
		}
		return "meal-menu";
	}
	
	/**
	 * 保存或修改菜肴
	 * @return
	 */
	public String mealMenuSaveOrUpdate() {
		Object img = getSession().getAttribute("imagePath");
		if (img != null) {
			images = img.toString();
		} 
		
		MealMenuDto mealMenu = new MealMenuDto(images, menuName, orderBy, price, introduce, isSell, isRecommend, special, mealMenuId, favorableId, taste, menuTypeId, getRestaurantId(), status);
		try {
			if (id != null && id != 0) {
				//MealMenuDto mmd = (MealMenuDto)super.serviceFacade.getMealMenuService().get(MealMenuDto.class, id); 
				mealMenu.setId(id);
				super.serviceFacade.getMealMenuService().update(mealMenu);
			} else {
				super.serviceFacade.getMealMenuService().save(mealMenu);
			}
			getSession().setAttribute("imagePath", null);
			getSession().removeAttribute("imagePath");
		} catch (ServiceException e) {
			logger.debug(e.getMessage(), e);
		} 
		requestURL = "mealmenu!mealMenuList.html";
		return SUCCESS;
	} 

	public MealMenuDto getMealMenuDto() {
		return mealMenuDto;
	}
	
	/**
	 * 删除本店菜单
	 * @return
	 */
	public String mealMenuDelete() {
		MealMenuDto mmd = new MealMenuDto();
		mmd.setId(id);
		try {
			super.serviceFacade.getMealMenuService().delete(mmd);
			println(0);
		} catch (ServiceException e) {
			logger.debug(e.getMessage(), e);
		} catch (IOException e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}

	/**
	 * 售完或未售完
	 * @return
	 */
	public String mealMenuSell() {
		try {
			super.serviceFacade.getMealMenuService().updateMealMenu(ids, isSell);
			println(0);
		} catch (ServiceException e) {
			logger.debug(e.getMessage(), e);
		} catch (IOException e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}
	
	public void setMealMenuDto(MealMenuDto mealMenuDto) {
		this.mealMenuDto = mealMenuDto;
	}

	public List<MealMenuDto> getMealMenuList() {
		return mealMenuList;
	}

	public void setMealMenuList(List<MealMenuDto> mealMenuList) {
		this.mealMenuList = mealMenuList;
	}

	public List<MealMenuTypeDto> getMealMenuTypeList() {
		return mealMenuTypeList;
	}

	public void setMealMenuTypeList(List<MealMenuTypeDto> mealMenuTypeList) {
		this.mealMenuTypeList = mealMenuTypeList;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getImages() {
		return images;
	}

	public void setImages(String images) {
		this.images = images;
	}

	public String getMenuName() {
		return menuName;
	}

	public void setMenuName(String menuName) {
		this.menuName = menuName;
	}

	public Integer getOrderBy() {
		return orderBy;
	}

	public void setOrderBy(Integer orderBy) {
		this.orderBy = orderBy;
	}

	public Double getPrice() {
		return price;
	}

	public void setPrice(Double price) {
		this.price = price;
	}

	public String getIntroduce() {
		return introduce;
	}

	public void setIntroduce(String introduce) {
		this.introduce = introduce;
	}

	public boolean isSell() {
		return isSell;
	}

	public void setSell(boolean isSell) {
		this.isSell = isSell;
	}

	public boolean isRecommend() {
		return isRecommend;
	}

	public void setRecommend(boolean isRecommend) {
		this.isRecommend = isRecommend;
	}

	public Double getSpecial() {
		return special;
	}

	public void setSpecial(Double special) {
		this.special = special;
	}

	public Integer getMealMenuId() {
		return mealMenuId;
	}

	public void setMealMenuId(Integer mealMenuId) {
		this.mealMenuId = mealMenuId;
	}

	public Integer getFavorableId() {
		return favorableId;
	}

	public void setFavorableId(Integer favorableId) {
		this.favorableId = favorableId;
	}

	public Integer getTaste() {
		return taste;
	}

	public void setTaste(Integer taste) {
		this.taste = taste;
	}

	public Integer getMenuTypeId() {
		return menuTypeId;
	}

	public void setMenuTypeId(Integer menuTypeId) {
		this.menuTypeId = menuTypeId;
	}

	public Integer getRestaurantId() {
		return restaurantId;
	}

	public void setRestaurantId(Integer restaurantId) {
		this.restaurantId = restaurantId;
	}

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}
}
